package com.example.demo.controller;

import com.example.demo.constant.Constant;
import com.example.demo.mapper.UserInfoMapper;
import com.example.demo.model.UserInfo;
import com.example.demo.service.UserInfoService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserContrller {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/redirect")
    public String redirect() {
        return "";
    }

    @RequestMapping("/check")
    public Boolean check(String userName, String password, HttpSession session) {
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }
        UserInfo userInfo = userInfoService.checkUserInfo(userName, password);
        if(userInfo != null && userInfo.getId() >= 0) {
            session.setAttribute(Constant.USER_SESSION_KEY, userInfo);
            return true;
        }
        return false;
    }
}
